<template>
  <div class="bg">
    <div class="content">
      <div class="left">
        <div class="main">
          <div class="struct">目录结构</div>
          <div class="el-struct">
            <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
          </div>
        </div>
      </div>
      <div class="right">
        <div class="main">
          <div class="add">
            <el-button type="primary" @click="addDialogFormVisible = true" size="small"
              icon="el-icon-circle-plus-outline">
              添加{{ buttomName }}子菜单
            </el-button>
            <el-dialog title="新增" :visible.sync="addDialogFormVisible" @close="close">
              <el-form :model="addFormData" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="菜单名" prop="name">
                  <el-input v-model="addFormData.name" placeholder="请输入菜单名"></el-input>
                </el-form-item>
                <el-form-item label="菜单链接" prop="path">
                  <el-input v-model="addFormData.path" placeholder="请输入菜单链接"></el-input>
                </el-form-item>
                <el-form-item label="图标" prop="icon">
                  <el-select v-model="addFormData.icon">
                    <el-option value="el-icon-share"><i class="el-icon-share"></i></el-option>
                    <el-option value="el-icon-zoom-out"><i class="el-icon-zoom-out"></i></el-option>
                    <el-option value="el-icon-goods"><i class="el-icon-goods"></i></el-option>
                    <el-option value="el-icon-user"><i class="el-icon-user"></i></el-option>
                    <el-option value="el-icon-bell"><i class="el-icon-bell"></i></el-option>
                    <el-option value="el-icon-setting"><i class="el-icon-setting"></i></el-option>
                    <el-option value="el-icon-refresh-right"><i class="el-icon-refresh-right"></i></el-option>
                    <el-option value="el-icon-paperclip"><i class="el-icon-paperclip"></i></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="描述" prop="note">
                  <el-input type="textarea" v-model="addFormData.note"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                  <el-input v-model="addFormData.sort"></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="state">
                  <el-select v-model="addFormData.state">
                    <el-option label="启用" value="启用"></el-option>
                    <el-option label="禁用" value="禁用"></el-option>
                  </el-select>
                </el-form-item>

              </el-form>

              <div slot="footer" class="dialog-footer">
                <el-button @click="addDialogFormVisible = false">Cancel</el-button>
                <el-button type="primary" @click="addSubmitForm('ruleForm')">OK</el-button>
              </div>
            </el-dialog>
          </div>
          <div class="el-table">
            <el-table :data="tableData.tableData" style="width: 100%">
              <el-table-column type="index" label="序号">
              </el-table-column>
              <el-table-column label="图标">
                <template slot-scope="scope">
                  <i :class="tableData.tableData[scope.$index].icon"></i>
                </template>
              </el-table-column>
              <el-table-column prop="name" label="菜单名称">
              </el-table-column>
              <el-table-column prop="note" label="描述">
              </el-table-column>
              <el-table-column prop="father" label="父级">
              </el-table-column>
              <el-table-column prop="state" label="状态">
              </el-table-column>
              <el-table-column label="操作" width="120">
                <template slot-scope="scope">
                  <div class="icon">
                    <div @click="look(scope.$index, scope.row)" class="first"><i class="el-icon-view"></i></div>
                    <div @click="updata(scope.$index)" class="child2"><i class="el-icon-setting"></i></div>
                    <div>
                      <el-popconfirm title="确定删除吗？" placement="top" @confirm="handleDelete(scope.$index)">
                        <div slot="reference"><i class="el-icon-delete"></i></div>
                      </el-popconfirm>
                    </div>
                  </div>
                </template>
              </el-table-column>
            </el-table>

            <!-- 查看 -->
            <el-dialog title="查看" :visible.sync="lookDialogFormVisible">
              <el-form :model="lookData" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="菜单名" prop="name">
                  <el-input v-model="lookData.name" placeholder="请输入菜单名" disabled></el-input>
                </el-form-item>
                <el-form-item label="菜单链接" prop="path">
                  <el-input v-model="lookData.path" placeholder="请输入菜单链接" disabled></el-input>
                </el-form-item>
                <el-form-item label="图标" prop="icon">
                  <el-select v-model="lookData.icon" disabled>
                    <el-option value="el-icon-share" disabled><i class="el-icon-share"></i></el-option>
                    <el-option value="el-icon-zoom-out" disabled><i class="el-icon-zoom-out"></i></el-option>
                    <el-option value="el-icon-goods" disabled><i class="el-icon-goods"></i></el-option>
                    <el-option value="el-icon-user" disabled><i class="el-icon-user"></i></el-option>
                    <el-option value="el-icon-bell" disabled><i class="el-icon-bell"></i></el-option>
                    <el-option value="el-icon-setting" disabled><i class="el-icon-setting"></i></el-option>
                    <el-option value="el-icon-refresh-right" disabled><i class="el-icon-refresh-right"></i></el-option>
                    <el-option value="el-icon-paperclip" disabled><i class="el-icon-paperclip"></i></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="描述" prop="note">
                  <el-input type="textarea" v-model="lookData.note" disabled></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                  <el-input v-model="lookIndex" disabled></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="state">
                  <el-select v-model="lookData.state" disabled>
                    <el-option label="启用" value="启用" disabled></el-option>
                    <el-option label="禁用" value="禁用" disabled></el-option>
                  </el-select>
                </el-form-item>

              </el-form>

              <div slot="footer" class="dialog-footer">
                <el-button @click="lookDialogFormVisible = false">Cancel</el-button>
                <el-button type="primary" @click="lookDialogFormVisible = false">OK</el-button>
              </div>
            </el-dialog>

            <!-- 编辑更新 -->
            <el-dialog title="修改" :visible.sync="updateDialogFormVisible">
              <el-form :model="updateData" :rules="rules" ref="ruleForm1" label-width="100px" class="demo-ruleForm">
                <el-form-item label="菜单名" prop="name">
                  <el-input v-model="updateData.name" placeholder="请输入菜单名"></el-input>
                </el-form-item>
                <el-form-item label="菜单链接" prop="path">
                  <el-input v-model="updateData.path" placeholder="请输入菜单链接"></el-input>
                </el-form-item>
                <el-form-item label="图标" prop="icon">
                  <el-select v-model="updateData.icon">
                    <el-option value="el-icon-share"><i class="el-icon-share"></i></el-option>
                    <el-option value="el-icon-zoom-out"><i class="el-icon-zoom-out"></i></el-option>
                    <el-option value="el-icon-goods"><i class="el-icon-goods"></i></el-option>
                    <el-option value="el-icon-user"><i class="el-icon-user"></i></el-option>
                    <el-option value="el-icon-bell"><i class="el-icon-bell"></i></el-option>
                    <el-option value="el-icon-setting"><i class="el-icon-setting"></i></el-option>
                    <el-option value="el-icon-refresh-right"><i class="el-icon-refresh-right"></i></el-option>
                    <el-option value="el-icon-paperclip"><i class="el-icon-paperclip"></i></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="描述" prop="note">
                  <el-input type="textarea" v-model="updateData.note"></el-input>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                  <el-input v-model="updateData.sort"></el-input>
                </el-form-item>
                <el-form-item label="状态" prop="state">
                  <el-select v-model="updateData.state">
                    <el-option label="启用" value="启用"></el-option>
                    <el-option label="禁用" value="禁用"></el-option>
                  </el-select>
                </el-form-item>

              </el-form>

              <div slot="footer" class="dialog-footer">
                <el-button @click="updateDialogFormVisible= false">Cancel</el-button>
                <el-button type="primary" @click="updateSubmitForm('ruleForm1')">OK</el-button>
              </div>
            </el-dialog>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import data from '../../data/data.js'
export default {
  data() {
    return {
      // tab栏数据
      data: data.order,
      defaultProps: {
        children: 'children',
        label: 'name'
      },

      //表格数据
      tableData: [],
      buttomName: '根级',

      //添加 弹窗
      addDialogFormVisible: false,
      addFormData: {
        icon: '',
        name: '',
        path: '',
        note: '',
        sort: null,
        father: '',
        state: '启用'
      },
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'blur' },
        ],
        path: [
          { required: true, message: '必填', trigger: 'blur' },
        ],
        sort: [
          { required: true, message: '请输入排序号', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (/^(?:[1-9]\d*)$/.test(value) == true || value === null) {
                callback();
              } else {
                callback(new Error("请输入正整数"));
              }
            },
            trigger: "change",
          },
        ]
      },

      //查看 弹窗
      lookDialogFormVisible: false,
      lookData: [],
      lookIndex: null,

      //编辑
      updateDialogFormVisible: false,
      updateData: {
        icon: '',
        name: '',
        path: '',
        note: '',
        sort: null,
        father: '',
        state: '启用'
      },
    }
  },
  methods: {
    // 点击tab
    handleNodeClick(data) {
      console.log(data);
      this.tableData = data
      this.buttomName = data.name
    },
    // 关闭 添加 弹窗
    close() {
      this.addFormData.icon = ''
      this.addFormData.name = ''
      this.addFormData.path = ''
      this.addFormData.note = ''
      this.addFormData.sort = null
      this.addFormData.father = ''
      this.addFormData.state = '启用'
    },
    addSubmitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.addDialogFormVisible = false;
          let dataObj = {}
          // this.$set(对象名,“属性名”,属性值)
          this.$set(dataObj, 'icon', this.addFormData.icon)
          this.$set(dataObj, 'name', this.addFormData.name)
          this.$set(dataObj, 'path', this.addFormData.path)
          this.$set(dataObj, 'note', this.addFormData.note)
          this.$set(dataObj, 'sort', this.addFormData.sort)
          this.$set(dataObj, 'father', this.addFormData.father)
          this.$set(dataObj, 'state', this.addFormData.state)

          let cDataObj = {
            name: '',
            tableData: [],
            children: []
          }
          cDataObj.name = this.addFormData.name
          // 数组.push(元素1,元素2,元素3.....元素n);
          // 数组.splice(index,howmany,item1,.....,itemN);
          if (dataObj.sort > this.tableData.length) {
            this.tableData.tableData.push(dataObj);
            this.tableData.children.push(cDataObj);
          } else {
            this.tableData.tableData.splice(dataObj.sort - 1, 0, dataObj);
            this.tableData.children.splice(dataObj.sort - 1, 0, cDataObj);
          }

          // console.log(this.tableData);
          this.$message({
            message: '添加成功',
            type: 'success'
          });

        } else {
          console.log(111);
          return false;
        }
      });
    },

    // 查看
    look(index, row) {
      this.lookDialogFormVisible = true;
      this.lookData = row
      this.lookIndex = index + 1
    },

    //删除
    handleDelete(index) {
      this.tableData.tableData.splice(index, 1)
      this.tableData.children.splice(index, 1)
      this.$message({
        type: 'success',
        message: '删除成功!'
      });
    },

    // 编辑
    updata(index) {
      this.updateDialogFormVisible = true;
      this.updateData.icon = this.tableData.tableData[index].icon
      this.updateData.name = this.tableData.tableData[index].name
      this.updateData.path = this.tableData.tableData[index].path
      this.updateData.note = this.tableData.tableData[index].note
      this.updateData.sort = index + 1
      this.updateData.father = this.tableData.tableData[index].father
      this.updateData.state = this.tableData.tableData[index].state

      this.updataIndex = index
    },
    updateSubmitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.updateDialogFormVisible = false;
          let dataObj = {};
          // this.$set(对象名,“属性名”,属性值)
          this.$set(dataObj, 'icon', this.updateData.icon)
          this.$set(dataObj, 'name', this.updateData.name)
          this.$set(dataObj, 'path', this.updateData.path)
          this.$set(dataObj, 'note', this.updateData.note)
          this.$set(dataObj, 'sort', this.updateData.sort)
          this.$set(dataObj, 'father', this.updateData.father)
          this.$set(dataObj, 'state', this.updateData.state)

          // 数组.push(元素1,元素2,元素3.....元素n);
          // 数组.splice(index,howmany,item1,.....,itemN);
          let cDataObj = {
            name: '',
            tableData: [],
            children: []
          }
          cDataObj.name = this.updateData.name
          this.tableData.tableData.splice(this.updataIndex, 1);
          this.tableData.children.splice(this.updataIndex, 1);
          // 数组.push(元素1,元素2,元素3.....元素n);
          // 数组.splice(index,howmany,item1,.....,itemN);
          if (dataObj.sort > this.tableData.length) {
            this.tableData.tableData.push(dataObj);
            this.tableData.children.push(cDataObj);
          } else {
            this.tableData.tableData.splice(dataObj.sort - 1, 0, dataObj);
            this.tableData.children.splice(dataObj.sort - 1, 0, cDataObj);
          }

          // console.log(this.tableData);
          this.$message({
            message: '修改成功',
            type: 'success'
          });

          this.updateData.name = '',
            this.updateData.code = '',
            this.updateData.describe = '',
            this.updateData.sort = null,
            this.updateData.state = '启用',
            this.updateData.give = []

        } else {
          return false;
        }
      });
    },
  }
}
</script>

<style lang="less" scoped>
.bg {
  background-color: #fff;
  height: calc(100vh - 100px);

  .content {
    // height: calc(100vh - 100px);
    width: 100%;
    // float: left;
    background-color: #fff;
    // padding: 16px;
    // overflow: auto;
    display: flex;

    .left {
      // float: left;
      // background-color: pink;
      padding: 16px;
      width: 256px;
      height: inherit;

      .main {
        width: 256px;
        min-height: 272px;
        border: 1px #f0f0f0 solid;
        height: 100%;

        .struct {
          height: 44px;
          border-bottom: 1px #f0f0f0 solid;
          padding: 0 10px;
          font-size: 14px;
          color: #000000;
          line-height: 44px;

          .el-struct {
            width: 100%;
          }
        }
      }
    }

    .right {
      // float: left;
      width: calc(100% - 304px);
      padding: 16px 16px 16px 0;
      // background-color: skyblue;
      width: 100%;
      flex: 1;
      overflow: hidden;

      .main {

        width: calc(100% - 5px);
        min-height: 272px;
        border: 1px #f0f0f0 solid;
        overflow: hidden;

        .add {
          height: 30px;
          width: 100%;
          padding: 10px;
        }

        .el-table {
          min-height: 222px;

          .icon {
            width: 80px;
            height: 15px;
            display: flex;

            div {
              height: 15px;
              flex: 1;
              font-size: 14px;
              text-align: center;
              line-height: 15px;
              cursor: pointer;
            }

            .first {
              color: #00a854;
              border-right: 1px #f0f0f0 solid;
            }

            .child2 {
              color: #0066cc;
              border-right: 1px #f0f0f0 solid;
            }

            div:last-child {
              color: #ff3333;
            }
          }
        }
      }
    }
  }
}
</style>